package be.dch.gui.i18n;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;

import org.apache.log4j.Logger;

public class UTF8Control extends Control {

    private static final Logger LOG = Logger.getLogger(UTF8Control.class);

    @Override
    public ResourceBundle newBundle(final String baseName, final Locale locale, final String format, final ClassLoader loader, final boolean reload) throws IllegalAccessException, InstantiationException, IOException {
	// The below is a copy of the default implementation.
	final String bundleName = this.toBundleName(baseName, locale);
	final String resourceName = this.toResourceName(bundleName, "properties");
	UTF8Control.LOG.debug("Loading bundle with name: " + resourceName + ".");
	ResourceBundle bundle = null;
	InputStream stream = null;
	if (reload) {
	    final URL url = loader.getResource(resourceName);
	    if (url != null) {
		final URLConnection connection = url.openConnection();
		if (connection != null) {
		    connection.setUseCaches(false);
		    stream = connection.getInputStream();
		}
	    }
	} else {
	    stream = loader.getResourceAsStream(resourceName);
	}
	if (stream != null) {
	    try {
		// Only this line is changed to make it to read properties files
		// as UTF-8.
		bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
	    } finally {
		stream.close();
	    }
	}
	return bundle;
    }
}